﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Extensions.Options;

namespace DX
{
    partial class FormRoutFactory : RouterFactory, IFormRoutFactory
    {

        public FormRoutFactory(IServiceProvider serviceProvider, IOptions<FormOption> options) : base(serviceProvider, options.Value)
        {

            option = options.Value;

        }
        readonly FormOption option;
        public void ShowDialog(Type viewModelType, string viewCode = null)
        {
            var view = CreateViewAndViewModel(viewModelType);

            view.ShowDialog();
        }

        public void Show(Type viewModelType, string viewCode = null)
        {
            var view = CreateViewAndViewModel(viewModelType);
            view.Show();
        }
        public void FindViewShow(Type viewModelType, string viewCode = null)
        {

            var form = FindView(viewModelType);
            if (form != null)
            {
                if (form.WindowState == FormWindowState.Normal)
                {
                    form.Activate();
                    if (option.EnableShake)
                    {
                        Point first = form.Location;
                        for (int i = 0; i < 8; i++)
                        {
                            Random ran = new Random();
                            Point p = new Point(form.Location.X + ran.Next(10) - 4, form.Location.Y +
                                ran.Next(10) - 4);
                            System.Threading.Thread.Sleep(10);
                            form.Location = p;
                        }
                        form.Location = first;
                    }
                }
                if (form.WindowState == FormWindowState.Minimized)
                {
                    form.WindowState = FormWindowState.Normal;
                }
                if (form.WindowState == FormWindowState.Maximized)
                {
                    form.Activate();
                }
                return;
            }
            Show(viewModelType, viewCode);
        }
    }
}
